#include "StdAfx.h"
#include "Object.h"
#include "Globals.h"

#include <algorithm>

Object::Object( Object* parent /*= NULL*/ )
{
	m_parent = parent;
	if(parent) parent->ChildInserted(this);
}

Object::~Object()
{
	if(m_parent) m_parent->ChildRemoving(this);
	DestroyChildren();
}

void Object::ChildInserted( Object* child )
{
	m_children.push_back(child);
}

void Object::ChildRemoving( Object* child )
{
	ObjectArray::iterator it = std::find(m_children.begin(), m_children.end(), child);
	if(it != m_children.end())
		m_children.erase(it);
}

void Object::DestroyChildren()
{
	ObjectArray::iterator it = m_children.begin();
	for(; it != m_children.end(); it++)
	{
		Object* child = *it;
		ASSERT(child);
		child->m_parent = NULL;
		delete child;
	}
	m_children.clear();
}

#ifdef _DEBUG

void Object::DumpSelf( Debugger& debugger )
{
}

void Object::CheckSelf()
{
}

#endif // _DEBUG
